package com.ruoyi.life.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.life.service.IFeBillService;
import com.ruoyi.omp.api.domain.FeBill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.life.mapper.SfRefundDetailMapper;
import com.ruoyi.life.domain.SfRefundDetail;
import com.ruoyi.life.service.ISfRefundDetailService;

/**
 * 退款明细Service业务层处理
 *
 * @author cjie
 * @date 2021-08-03
 */
@Service
public class SfRefundDetailServiceImpl implements ISfRefundDetailService {
    @Autowired
    private SfRefundDetailMapper sfRefundDetailMapper;

    @Autowired
    private IFeBillService feBillService;

    /**
     * 查询退款明细
     *
     * @param id 退款明细ID
     * @return 退款明细
     */
    @Override
    public SfRefundDetail selectSfRefundDetailById(String id) {
        return sfRefundDetailMapper.selectSfRefundDetailById(id);
    }

    /**
     * 查询退款明细列表
     *
     * @param sfRefundDetail 退款明细
     * @return 退款明细
     */
    @Override
    public List<SfRefundDetail> selectSfRefundDetailList(SfRefundDetail sfRefundDetail) {
        return sfRefundDetailMapper.selectSfRefundDetailList(sfRefundDetail);
    }

    /**
     * 新增退款明细
     *
     * @param sfRefundDetail 退款明细
     * @return 结果
     */
    @Override
    public int insertSfRefundDetail(SfRefundDetail sfRefundDetail) {
        sfRefundDetail.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        sfRefundDetail.setCreateTime(DateUtils.getNowDate());
        sfRefundDetail.setCreateBy(SecurityUtils.getUsername());
        return sfRefundDetailMapper.insertSfRefundDetail(sfRefundDetail);
    }

    /**
     * 修改退款明细
     *
     * @param sfRefundDetail 退款明细
     * @return 结果
     */
    @Override
    public int updateSfRefundDetail(SfRefundDetail sfRefundDetail) {
        FeBill feBill = feBillService.selectFeBillById(sfRefundDetail.getBillId());
        //退款后实收金额减去退款金额
        feBill.getPayAmount().subtract(sfRefundDetail.getRefundAmount());
        sfRefundDetail.setUpdateTime(DateUtils.getNowDate());
        sfRefundDetail.setUpdateBy(SecurityUtils.getUsername());
        return sfRefundDetailMapper.updateSfRefundDetail(sfRefundDetail);
    }

    /**
     * 批量删除退款明细
     *
     * @param ids 需要删除的退款明细ID
     * @return 结果
     */
    @Override
    public int deleteSfRefundDetailByIds(String[] ids) {
        return sfRefundDetailMapper.deleteSfRefundDetailByIds(ids);
    }

    /**
     * 删除退款明细信息
     *
     * @param id 退款明细ID
     * @return 结果
     */
    @Override
    public int deleteSfRefundDetailById(String id) {
        return sfRefundDetailMapper.deleteSfRefundDetailById(id);
    }
}
